package com.song.user.controller;

import com.song.api.BaseController;
import com.song.api.controller.user.MyFansControllerApi;
import com.song.grace.result.GraceJSONResult;
import com.song.grace.result.ResponseStatusEnum;
import com.song.pojo.vo.FansCountsVO;
import com.song.pojo.vo.RegionRatioVO;
import com.song.user.service.MyFansService;
import com.song.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class MyFansController extends BaseController implements MyFansControllerApi {

    private static final Logger LOG = LoggerFactory.getLogger(MyFansController.class);

    @Autowired
    private MyFansService myFansService;

    @Override
    public GraceJSONResult isMeFollowThisWriter(String writerId, String fanId) {

        // 参数判空
        if (StringUtils.isBlank(writerId) && StringUtils.isBlank(fanId)) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FANS_ATTENTION_ERROR);
        }

        Boolean result = myFansService.isMeFollowThisWriter(writerId, fanId);
        return GraceJSONResult.ok(result);
    }

    @Override
    public GraceJSONResult follow(String writerId, String fanId) {

        // 参数判空
        if (StringUtils.isBlank(writerId) && StringUtils.isBlank(fanId)) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FANS_ATTENTION_ERROR);
        }

        myFansService.follow(writerId, fanId);
        return GraceJSONResult.ok();
    }

    @Override
    public GraceJSONResult unfollow(String writerId, String fanId) {
        // 参数判空
        if (StringUtils.isBlank(writerId) && StringUtils.isBlank(fanId)) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FANS_ATTENTION_ERROR);
        }

        myFansService.unfollow(writerId, fanId);
        return GraceJSONResult.ok();
    }

    @Override
    public GraceJSONResult queryAll(String writerId, Integer page, Integer pageSize) {
        // 参数判空
        if (StringUtils.isBlank(writerId)) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FANS_QUERY_PARAMS_ERROR);
        }

        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult pagedGridResult = myFansService.queryAllFansESList(writerId, page, pageSize);

        return GraceJSONResult.ok(pagedGridResult);
    }

    @Override
    public GraceJSONResult queryRatio(String writerId) {

        if (StringUtils.isBlank(writerId)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FANS_QUERY_PARAMS_ERROR);
        }
//
//        // 查询男粉丝数
//        Integer manCounts = myFansService.queryFansCounts(writerId, Sex.man);
//        // 查询女粉丝数
//        Integer womanCounts = myFansService.queryFansCounts(writerId, Sex.woman);
//
//        FansCountsVO fansCountsVO = new FansCountsVO();
//        fansCountsVO.setManCounts(manCounts);
//        fansCountsVO.setWomanCounts(womanCounts);
        FansCountsVO fansCountsVO = myFansService.queryFansESCounts(writerId);

        return GraceJSONResult.ok(fansCountsVO);
    }

    @Override
    public GraceJSONResult queryRatioByRegion(String writerId) {

        //参数判空
        if (StringUtils.isBlank(writerId)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.FANS_QUERY_PARAMS_ERROR);
        }

        List<RegionRatioVO> list = myFansService.queryRatioByRegionESCounts(writerId);

        return GraceJSONResult.ok(list);
    }

    @Override
    public GraceJSONResult forceUpdateFanInfo(String relationId, String fanId) {
        myFansService.forceUpdateFanInfo(relationId,fanId);
        return GraceJSONResult.ok();
    }
}
